<!DOCTYPE html>
<html>
	{include file="public/_header" /}
	<body>
		<div class="x-nav">
			<span class="layui-breadcrumb">
				<a href="">首页</a>
				<a>
					<cite>幻灯片</cite></a>
			</span>
			<a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" onclick="location.reload()"
			 title="刷新">
				<i class="layui-icon layui-icon-refresh" style="line-height:30px"></i></a>
		</div>
		<div class="layui-fluid">
			<div class="layui-row layui-col-space15">
				<div class="layui-col-md12">
					<div class="layui-card">
						<div class="layui-card-body ">
							<table class="layui-hide" id="test" lay-filter="test"></table>
						</div>
					</div>
				</div>
			</div>
		</div>

		<script type="text/html" id="toolbarDemo">
			<div class="layui-btn-container">
					<button class="layui-btn layui-btn-sm" lay-event="getCheckData">
						 <i class="layui-icon">&#xe608;</i>{:lang('add')}
					</button>
					<button class="layui-btn layui-btn-sm layui-btn-warm"  lay-event="type">
						<i class="layui-icon">&#xe60a;</i>{:lang('nametype')}
					</button>
					<button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="getCheckLength">
						<i class="layui-icon">&#xe642;</i>{:lang('delete')}
					</button>
				</div>
		</script>

		<script type="text/html" id="barDemo">
			<a class="layui-btn layui-btn-xs" lay-event="edit">{:lang('edit')}</a>
		  <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">{:lang('delete')}</a>
		</script>

		<script>
			function check_validate1(value){
			    //定义正则表达式部分
			    var reg = /^\d+$/;
			    if( value.match( reg )){
			        return true;
			    }
			    return false;
			}
			var url = "__STATIC__/admin/"
			layui.use('table', function() {
				var table = layui.table;
				var form = layui.form;
				var tableIns = table.render({
					elem: '#test',
					url: "{:url('admin/Banner/banner_lists')}",
					toolbar: '#toolbarDemo',
					title: '用户数据表',
					cols: [
						[{
							type: 'checkbox',
							fixed: 'left',
							width: 80
						}, {
							field: 'id',
							title: 'ID',
							width: 80,
							fixed: 'left',
							unresize: true,
							sort: true
						}, {
							field: 'sort',
							title: "{:lang('sort')}",
							width: 100,
							edit: true,
							sort: true
						}, {
							field: 'title',
							title: "{:lang('title')}"
						}, {
							field: 'url',
							title: "{:lang('url')}",
							templet: function(res) {
								return '<span>' + res.url + '</span>'
							}
						}, {
							field: 'name',
							title: "{:lang('nametype')}",
							sort: true
						}, {
							field: 'logo',
							title: "{:lang('logo')}",
							templet: function(d) {
								if (d.logo) {
									var logoSrc = "<img src='/" + d.logo + "'  width='35' height='26' style='border:1px solid #ccc;box-sizing:border-box' />";
								} else {
									var logoSrc = "<img src='" + url + "images/imageno.png' width='40' />";
								}
								return logoSrc;
							},
							event:"nameClick"
						}, {
							field: 'is_check',
							title: "{:lang('check')}",
							templet: function(d) {
								if (d.is_check == 1) {
									var check = "<input type='checkbox' value='"+d.id+"' lay-filter='is_check' lay-skin='switch' lay-text='{:lang("show")}|{:lang("hide")}' checked>";
								} else if(d.is_check == 0) {
									var check = "<input type='checkbox' value='"+d.id+"' lay-filter='is_check' lay-skin='switch' lay-text='{:lang("show")}|{:lang("hide")}'>";
								}
							
								return check;
							}
						},{
							fixed: 'right',
							title: "{:lang('action')}",
							toolbar: '#barDemo',
							width: 150
						}]
					],
					id: 'testReload',
					page: true
				});

				//头工具栏事件
				table.on('toolbar(test)', function(obj) {
					var checkStatus = table.checkStatus(obj.config.id);
					switch (obj.event) {
						case 'getCheckData':
							parent.layer.open({
								title: "{:lang('add')}",
								type: 2,
								area: ['800px', '680px'],
								content: "{:url('admin/Banner/banneradd')}",
								success:function(){
									top.layerFrameConfig.iframeB = {
										info:tableIns
									}
								}
							});
							break;
						case 'getCheckLength':
							var data = checkStatus.data;
							
							if(data.length<1){
								layer.msg('您最少选择一个选项进行操作');
								return false;
							}
							var ids = [];
							data.forEach(function(item,index){
								ids.push(item.id)
							})
							layer.confirm('确认要删除这些内容？', function(){
							  $.post("{:url('admin/Banner/banner_all_dele')}",{ids:ids},function(res){
							  	if(res.status == 1){
							  		layer.msg('删除成功', {
							  			icon: 1,
							  			time: 300
							  		}, function() {
							  			tableIns.reload()
							  		});
							  	}else{
							  		layer.msg(res.message)
							  	}
							  })
							});
							
							break;
						case 'type':
							parent.layer.open({
								title: "{:lang('typelist')}",
								type: 2,
								area: ['900px', '600px'],
								content: "{:url('admin/Banner/typelists')}",
								success: function(layero, index) {
									top.layerFrameConfig.iframeB = {
										layer_index: index
									}
								}
							});
							break;
					};
				});

				//监听行工具事件
				table.on('tool(test)', function(obj) {
					var data = obj.data;
					//console.log(obj)
					if (obj.event === 'del') {
						layer.confirm('真的删除行么', function(index) {
							$.post("{:url('admin/Banner/bannerdele')}",data,function(res){
								if(res.status == 1){
									layer.msg('删除成功', {
										icon: 1,
										time: 300
									}, function() {
										layer.close(index);
										tableIns.reload()
									});
								}else{
									layer.msg(res.message)
								}
							})
							
						});
					} else if (obj.event === 'edit') {
						parent.layer.open({
							title: '编辑',
							type: 2,
							area: ['800px', '680px'],
							content: "{:url('admin/Banner/banneredit')}?id="+data.id,
							success:function(){
								top.layerFrameConfig.iframeB = {
									info:tableIns
								}
							}
						});
					}else if(obj.event === 'nameClick'){
						layer.photos({
							anim: 0,
							shade: [0.1, '#333'],
						    photos: { "data": [{"src": '/'+obj.data.logo}] }
						});
					}
				});
				//监听单元格编辑
				table.on('edit(test)', function(obj) {
					var value = obj.value, //得到修改后的值
						data = obj.data, //得到所在行所有键值
						field = obj.field; //得到字段
						if(!check_validate1(value)){
							layer.msg('你必须输入数字');
							tableIns.reload()
						}else{
							sort(obj,tableIns)
						}
						
					
				});
				form.on('switch(is_check)', function(obj){
					// console.log(this.value,this.name,obj.elem.checked,obj.othis)
					var data = {
						id:this.value,
						status:obj.elem.checked?1:0
					}
					$.post("{:url('admin/Banner/banner_check')}",data,function(res){
						if(res.status == 1){
							layer.msg('设置成功');
						}else{
							layer.msg(res.message);
							tableIns.reload()
						}
					})
				});      
				  
			});
			function sort(obj,tableIns){
				$.post("{:url('admin/Banner/bannersort')}",obj.data,function(res){
					if(res.status == 1){
						tableIns.reload()
					}
				})
			}
		</script>
	</body>
</html>
